<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/11/2
 * TIME: 22:32
 */

namespace app\admin\model;


use think\facade\Cookie;

class User extends BaseModel {

    public static function getUserByName($name) {
        $user = self::where('username', $name)->find();
        return $user;
    }

    public static function login($data) {
        $user = self::getUserByName($data['username']);
        $login = [
            'msg'  => '账号密码错误',
            'code' => 400
        ];
        if ($user['password'] == md5($data['password'])) {
            $login['code'] = 200;
            Cookie::set('username',$user['username'],3600);
            return $login;
        } else {
            return $login;
        }
    }

    public static function register2($data) {
        $user = self::getUserByName($data['username']);
        $msg = [
            'code' => 400
        ];
        if ($user == null) {
            $user_object = self::create([
                'username' => $data['username'],
                'password' => md5($data['password'])
            ]);
            $msg['code'] = 200;
        } else {
            $msg['msg']['exist'] = '用户名已存在！';
        }
        return $msg;
    }
}